package com.ruoyi.basis.utils.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mashuai.utils.base.MsEmptyUtils;
import com.mashuai.utils.toolkit.core.msConverts.MsConvertDataUtils;
import com.ruoyi.common.core.domain.BaseEntity;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 查询条件构造器
 *
 * @ClassName: MpConditonUtils
 * @Author : mashuai
 * @Description: TODO
 */
public class MpConditionUtils {
    //不参与条件构造器的字段
    private static final String excludeFiledName = "pageNum,pageSize";

    /**
     * 获取查询参数
     *
     * @param params {"pageNum":1,"pageSize":10}
     *               其他参数比如 name:"张三",name@eq:"张三","name@like":"张三","name@in":张三,李四
     *               age@ne:12,age@le:12...
     *               time@between:"2024-02-28,2024-02-39"
     * @param clz
     * @param <T>
     * @return
     */
    public static <T extends BaseEntity> QueryWrapper<T> getQueryWrapper(Map<String, Object> params, Class<T> clz) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        for (String key : params.keySet()) {
            String condition = "like";
            String fieldName = key;
            if (key.contains("@")) {
                String[] split = key.split("@");
                fieldName = split[0];
                condition = split[1];
            }
            if (!excludeFiledName.contains(fieldName)) {
                String columnName = getColumn(fieldName, clz);
                getQueryWrapper(qw, condition, columnName, params.get(key));
            }
        }
        return qw;
    }


    /**
     * 获取字段对应的表字段名称，后面做缓存
     **/
    private static <T extends BaseEntity> String getColumn(String key, Class<T> clz) {
        Field[] declaredFields = clz.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (!excludeFiledName.contains(fieldName) && fieldName.equals(key)) {
                //驼峰转下划线
                return MsConvertDataUtils.camLToUnderline(fieldName);
            }
        }
        return null;
    }


    /**
     * 获取条件构造器，目前仅支持一些简单的
     **/
    private static <T extends BaseEntity> void getQueryWrapper(QueryWrapper<T> qw, String condition, String fieldName, Object val) {
        if (MsEmptyUtils.isEmpty(val)) return;
        if (condition.equals("eq")) {
            qw.eq(fieldName, val);
        } else if (condition.equals("in")) {
            String[] valList = String.valueOf(val).split(",");
            qw.in(fieldName, valList);
        } else if (condition.equals("between")) {
            String[] split = String.valueOf(val).split(",");
            qw.between(fieldName, split[0], split[1]);
        } else if (condition.equals("ne")) {
            qw.ne(fieldName, val);
        } else if (condition.equals("notLike")) {
            qw.notLike(fieldName, val);
        } else if (condition.equals("gt")) {
            qw.gt(fieldName, val);
        } else if (condition.equals("ge")) {
            qw.ge(fieldName, val);
        } else {
            qw.like(fieldName, val);
        }
    }
}
